//
//  UILabel+additional.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UILabel+additional.h"

@implementation UILabel (additional)

+ (UILabel *)labelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor
{
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, font.lineHeight) lineBreakMode:NSLineBreakByTruncatingTail];
    return [UILabel labelWithText:text font:font textColor:textColor width:size.width numberOfLines:1 lineBreakMode:NSLineBreakByTruncatingTail];
}

+ (UILabel *)labelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor width:(CGFloat)width
{
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = font;
    label.textColor = textColor;
    label.text = text;
    
    if (text && 0 != [text length])
    {
        width = [UILabel fitSize:width text:text font:font numberOfLines:1 lineBreakMode:UILineBreakModeCharacterWrap].width;
    }

    label.frame = CGRectMake(0, 0, width, font.lineHeight);
    
    return label;
}

+ (UILabel *)labelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor width:(CGFloat)width numberOfLines:(NSUInteger)numberOfLines lineBreakMode:(UILineBreakMode)linBreakMode
{
    CGSize size = [UILabel fitSize:width text:text font:font numberOfLines:numberOfLines lineBreakMode:linBreakMode];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, size.height)] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.text = text;
    label.font = font;
    label.textColor = textColor;
    label.numberOfLines = numberOfLines;
    label.lineBreakMode = linBreakMode;
    
    return label;
}

+ (CGSize)fitSize:(CGFloat)aWidth text:(NSString*)aText font:(UIFont*)aFont numberOfLines:(NSInteger)aNumberOfLines lineBreakMode:(UILineBreakMode)aLineBreakMode
{
    CGFloat maxHeight = (0 == aNumberOfLines) ? CGFLOAT_MAX : aFont.lineHeight * aNumberOfLines;
    CGSize size = [aText sizeWithFont:aFont constrainedToSize:CGSizeMake(aWidth, maxHeight) lineBreakMode:aLineBreakMode];
    if ([aText respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              aFont, NSFontAttributeName,
                                              nil];
        CGRect rect = [aText boundingRectWithSize:CGSizeMake(aWidth, maxHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
        size = CGSizeMake(rect.size.width + 1, rect.size.height + 1);
    }
    return size;
}

- (CGSize)fitSize:(CGFloat)aWidth
{
    CGFloat maxHeight = (0 == self.numberOfLines) ? CGFLOAT_MAX : self.font.lineHeight * self.numberOfLines;
    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(aWidth, maxHeight) lineBreakMode:self.lineBreakMode];
    return size;
}

- (CGFloat)fitWidth:(CGFloat)aHeight
{
    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, aHeight) lineBreakMode:self.lineBreakMode];
    return size.width;
}
@end
